package games.car;


import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Random;

public class Car {

    private int id;
    private int x, y, oldx, oldy;
    private int xspeed = Config.CAR_DEFAULT_SPEED;
    private int yspeed = Config.CAR_DEFAULT_SPEED;
    private boolean bU = false, bD = false, bL = false, bR = false;
    private boolean isPlayer = false;
    private boolean live = true;
    private Image image = null;

    private enum direction {
        U, D, L, R, STOP
    }

    private direction dir = direction.STOP;

    public Car(int x, int y){
        this.x = x;
        this.y = y;
        this.oldx = x;
        this.oldy = y;
        int i = new Random().nextInt(6);
        image = Images.CAR_IMG_ARR[i];
    }

    public Car(int x, int y,int yspeed){
        this.x = x;
        this.y = y;
        this.oldx = x;
        this.oldy = y;
        this.yspeed = yspeed;
        int i = new Random().nextInt(6);
        image = Images.CAR_IMG_ARR[i];
    }

    public Car(int x, int y,boolean isPlayer){
        this.x = x;
        this.y = y;
        this.oldx = x;
        this.oldy = y;
        this.isPlayer = isPlayer;
        image = Images.CAR_IMG;
    }

    void draw(Graphics g) {
        g.drawImage(image, x, y,Config.CAR_WIDTH,Config.CAR_HEIGHT,null);

        move();
    }

    private void move() {
        this.oldx = x;
        this.oldy = y;
        if(isPlayer){
            switch (dir) {
                case U:
                    if (y > Config.FRAME_WIDTH + yspeed + 25)
                        y -= yspeed;
                    break;
                case D:
                    if (y < Config.WINDOW_HEIGHT - Config.FRAME_WIDTH - yspeed - Config.CAR_HEIGHT)
                        y += yspeed;
                    break;
                case L:
                    if (x > Config.FRAME_WIDTH + yspeed)
                        x -= xspeed;
                    break;
                case R:
                    if (x < Config.WINDOW_WIDTH - Config.FRAME_WIDTH - yspeed - Config.CAR_WIDTH)
                        x += xspeed;
                    break;
                case STOP:
                    break;
            }
        }else{
            if (y < Config.WINDOW_HEIGHT - Config.FRAME_WIDTH - yspeed){
                y += yspeed;
            }else{
                this.live = false;
            }
        }
    }

    void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        switch (key) {
            case KeyEvent.VK_W:
            case KeyEvent.VK_UP:
                bU = true;
                break;
            case KeyEvent.VK_S:
            case KeyEvent.VK_DOWN:
                bD = true;
                break;
            case KeyEvent.VK_A:
            case KeyEvent.VK_LEFT:
                bL = true;
                break;
            case KeyEvent.VK_D:
            case KeyEvent.VK_RIGHT:
                bR = true;
                break;

        }
        direction();
    }

    void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        switch (key) {
            case KeyEvent.VK_W:
            case KeyEvent.VK_UP:
                bU = false;
                break;
            case KeyEvent.VK_S:
            case KeyEvent.VK_DOWN:
                bD = false;
                break;
            case KeyEvent.VK_A:
            case KeyEvent.VK_LEFT:
                bL = false;
                break;
            case KeyEvent.VK_D:
            case KeyEvent.VK_RIGHT:
                bR = false;
                break;
        }
        direction();
    }

    private void direction() {
        if (bU)
            dir = direction.U;
        else if (bD)
            dir = direction.D;
        else if (bL)
            dir = direction.L;
        else if (bR)
            dir = direction.R;
        else if (!bU && !bD && !bL && !bR)
            dir = direction.STOP;
    }

    boolean isLive(){
        return live;
    }

    void dead(){
        this.live = false;
    }

    boolean isPlayer(){
        return isPlayer;
    }

    Rectangle getRect() {
        return new Rectangle(x, y, (int)(Config.CAR_WIDTH * 0.75), (int)(Config.CAR_HEIGHT * 0.75));
    }

    boolean collision(Car car) {
        if (this != car && this.live && car.live) {
            if (this.getRect().intersects(car.getRect())) {
                this.live = false;
                if(Config.soundOn){
                    new Voice(Config.BOOM_VOICE).start();
                }
                return true;
            }
        }
        return false;
    }

    int getX(){
        return this.x;
    }

    int getY(){
        return this.y;
    }

    void speedUp(){
        xspeed = Config.CAR_DEFAULT_SPEED + 1;
        yspeed = Config.CAR_DEFAULT_SPEED + 1;
    }

    void speedReset(){
        xspeed = Config.CAR_DEFAULT_SPEED;
        yspeed = Config.CAR_DEFAULT_SPEED;
    }
}
